package bid.yanhj.common.service;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import bid.yanhj.common.util.Constants;
import bid.yanhj.common.util.LocalMap;
import bid.yanhj.common.util.MetaInfo;

/**
 * 
 * @author yanhaijiang
 *
 */
public class CacheService {
	/**
	 * log.
	 */
	private static final Logger LOG = LoggerFactory.getLogger(CacheService.class);

	/**
	 * Constructor.
	 */
	private CacheService() {

	}

	/**
	 * 
	 * @param info
	 *            info
	 */
	public static void putMetaInfo(MetaInfo info) {
		if (info != null) {
			LocalMap.contextMap().put(Constants.CacheKey.META_KEY, info);
		}
	}

	/**
	 * getMetaInfo.
	 * 
	 * @return MetaInfo
	 */
	public static MetaInfo getMetaInfo() {
		Map<String, Object> map = LocalMap.contextMap();
		MetaInfo meta = null;
		if (map != null) {
			meta = (MetaInfo) map.get(Constants.CacheKey.META_KEY);
		}
		return meta;
	}

	/**
	 * getResult.
	 * 
	 * @return Object
	 */
	public static Object getResult() {
		Map<String, Object> map = LocalMap.contextMap();
		if (map != null) {
			return map.get(Constants.CacheKey.RESULT_KEY);
		}
		return null;
	}
	
    /**
     * putResult.
     * 
     * @param obj object
     */
	public static void putResult(Object obj) {
		try {
			Map<String, Object> map = LocalMap.contextMap();
			map.put(Constants.CacheKey.RESULT_KEY, obj);
		} catch (RuntimeException e) {
            LOG.error(FormatService.logFormat("putResult error " + e.getMessage()));
		}
	}
	
    /**
     * putParamters.
     * 
     * @param obj object
     */
    public static void putParamters(Object obj) {
        try {
            Map<String, Object> map = LocalMap.contextMap();
            map.put(Constants.CacheKey.REQUEST_PARAMTERS_KEY, obj);
        } catch (RuntimeException e) {
            LOG.error(FormatService.logFormat("putResult error " + e.getMessage()));
        }
    }

    /**
     * getParamters.
     * 
     * @return Object
     */
    public static Object getParamters() {
        Map<String, Object> map = LocalMap.contextMap();
        if (map != null) {
            return map.get(Constants.CacheKey.REQUEST_PARAMTERS_KEY);
        }
        return null;
    }

    /**
     * 
     * @param key String
     * @param value Object
     */
    public static void put(String key, Object value) {
        if (key != null && value != null) {
            try {
                Map<String, Object> map = LocalMap.contextMap();
                map.put(key, value);
            } catch (RuntimeException e) {
                LOG.error(FormatService.logFormat("put error " + e.getMessage()));
            }
        }
    }

    /**
     * 
     * @param key String
     * @return Object
     */
    public static Object get(String key) {
        Map<String, Object> map = LocalMap.contextMap();
        if (map != null) {
            return map.get(key);
        }
        return null;
    }
}
